import { StoreOptions } from "vuex";
import ACCESS_EUNM from "@/access/ACCESS_EUNM";
import { UserControllerService } from "../../generated";

export default {
  namespaced: true,
  //设置全局变量state里面存储的数据以及其类型
  state: () => ({
    loginUser: {
      userName: "未登录",
    },
  }),
  //便捷访问state里面的数据

  //执行异步操作，并且触发mutations的更改
  actions: {
    async getLoginUser({ commit, state }, payload) {
      const res = await UserControllerService.getLoginUserUsingGet();
      if (res.code === 0) {
        commit("updateUser", res.data);
      } else {
        commit("updateUser", {
          ...state.loginUser,
          userRole: ACCESS_EUNM.LOGIN_FAILURE,
        });
      }
    },
  },
  //定义了对变量进行增删改的方法
  mutations: {
    updateUser(state, payload) {
      state.loginUser = payload;
    },
  },
} as StoreOptions<any>;
